/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package clienteddns;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.*;
import java.security.Security;
import java.util.Enumeration;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;

/**
 *
 * @author roberto
 */
public class ClienteDDNS {

    public void registerMyHostnameVerifier() {
        javax.net.ssl.HostnameVerifier myHv = new javax.net.ssl.HostnameVerifier() {

            public boolean verify(String hostName, javax.net.ssl.SSLSession session) {
                return true;
            }
        };
        javax.net.ssl.HttpsURLConnection.setDefaultHostnameVerifier(myHv);
    }

    public String getValue(BufferedReader br) throws IOException {
        try {
            String linha = "";
            while ((linha = br.readLine()) != null) {
               // System.out.println("\n"+linha+"\n");
                if (linha.equals("<body>")) {
                    linha = br.readLine();
                 //     System.out.println("\n"+linha+"\n");
                    return linha;
                }
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return null;
    }

    public boolean validateIp(String c) {
        String s[] = c.split("\\.");
        if (s.length == 4) {
            for (int i = 0; i < s.length; i++) {
                //System.out.println(s[i]);
                if (Integer.parseInt(s[i]) <= 255) {
                    continue;
                } else {
                    return false;
                }
            }
        } else {
            return false;
        }
        return true;
    }
    
    public String getIpInterno() {
        String ip = new String();
        ClienteDDNS c = new ClienteDDNS();
        Enumeration nis = null;
        try {
            nis = NetworkInterface.getNetworkInterfaces();
        } catch (SocketException e) {
            e.printStackTrace();
        }


        while (nis.hasMoreElements()) {

            NetworkInterface ni = (NetworkInterface) nis.nextElement();
            Enumeration ias = ni.getInetAddresses();
            while (ias.hasMoreElements()) {
                InetAddress ia = (InetAddress) ias.nextElement();
                if (ni.getName().equals("wlan0")) {
                    ip = ia.getHostAddress();
                    //          System.out.println(ip);
                    if (c.validateIp(ip)) {
                        break;
                    }
                }
            }
        }

        return ip;
    }
}